package com.inossem.allotin.utils;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Display;
import android.view.WindowManager;

import static android.content.Context.WINDOW_SERVICE;

/**
 * 屏幕适配工具类
 *
 * @author 詹建宇
 * @time on 2018/3/12 14:44
 * @email jianyu.zhan@inossem.com
 */
public class DensityHelper {

    public static final float WIDTH_720P = 720;
    public static final float WIDTH_750P = 750;
    public static final float WIDTH_1080P = 1080;
    public static final float WIDTH_1440P = 1440;
    public static final float WIDTH_1920P = 1920;

    private static Application.ActivityLifecycleCallbacks activityLifecycleCallbacks;

    /**
     * 初始化
     *
     * @param application application
     * @param width       设计稿宽度尺寸
     */
    public static void initHelper(Application application, float width) {

        activityLifecycleCallbacks = new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                //通常情况下application与activity得到的resource虽然不是一个实例，但是displayMetrics是同一个实例，只需调用一次即可
                //为了面对一些不可预计的情况以及向上兼容，分别调用一次较为保险
                resetDensity(application, width);
                resetDensity(activity, width);
            }

            @Override
            public void onActivityStarted(Activity activity) {
                resetDensity(application, width);
                resetDensity(activity, width);
            }

            @Override
            public void onActivityResumed(Activity activity) {
                resetDensity(application, width);
                resetDensity(activity, width);
            }

            @Override
            public void onActivityPaused(Activity activity) {
                resetDensity(application, width);
                resetDensity(activity, width);
            }

            @Override
            public void onActivityStopped(Activity activity) {
                resetDensity(application, width);
                resetDensity(activity, width);
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                resetDensity(application, width);
                resetDensity(activity, width);
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                resetDensity(application, width);
                resetDensity(activity, width);
            }
        };

        activate(application, width);
    }

    /**
     * 激活本方案
     */
    private static void activate(Application application, float width) {
        resetDensity(application, width);
        application.registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
    }

    /**
     * 重新计算displayMetrics.xhdpi, 使单位pt重定义为设计稿的相对长度
     *
     * @param context     context
     * @param designWidth 设计稿的宽度
     */
    private static void resetDensity(Context context, float designWidth) {
        WindowManager manager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
        if (manager == null) return;
        Display display = manager.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        context.getResources().getDisplayMetrics().xdpi = size.x / designWidth * 72f;
    }

    /**
     * 转换dp为px
     *
     * @param context context
     * @param value   需要转换的dp值
     * @return px
     */
    public static float dp2px(Context context, float value) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
    }

    /**
     * 转换pt为px
     *
     * @param context context
     * @param value   需要转换的pt值，若context.resources.displayMetrics经过resetDensity()的修改则得到修正的相对长度，否则得到原生的磅
     * @return px
     */
    public static float pt2px(Context context, float value) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, value, context.getResources().getDisplayMetrics());
    }

    /**
     * 转换px为pt
     */
    public static float px2pt(Context context, float value) {
        return value / pt2px(context, 1);
    }

}
